package com.lyn.solitude.plugin.adapter

import android.annotation.SuppressLint
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.lyn.solitude.plugin.adapter.viewHolder.ViewBindingHolder

/**
 * recyclerView 适配器
 * @author longyn 2024/05/16
 * @version 1.0.0
 */
abstract class BaseAdapter<V> : RecyclerView.Adapter<ViewBindingHolder>() {

    private val data: MutableList<V> = mutableListOf()
    var onItemListener: OnItemListener<V>? = null
    var onEmptyListener: OnEmptyListener? = null


    @SuppressLint("NotifyDataSetChanged")
    fun setData(data: List<V>) {
        this.data.clear()
        this.data.addAll(data)
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun addItem(v: V, clear: Boolean = false) {
        if (clear) {
            this.data.clear()
        }
        this.data.add(v)
        if (clear) {
            notifyDataSetChanged()
        } else {
            notifyItemInserted(this.data.size - 1)
        }
    }

    fun getItem(position: Int): V? {
        try {
            return this.data[position]
        } catch (_: Throwable) {
        }
        return null
    }

    fun removeItem(position: Int) {
        try {
            this.data.removeAt(position)
            notifyItemRemoved(position)
        } catch (e: Throwable) {
            e.printStackTrace()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun clear() {
        this.data.clear()
        notifyDataSetChanged()
    }

    override fun getItemViewType(position: Int): Int {
        if (this.data.isEmpty()) {
            return -1
        }
        return super.getItemViewType(position)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewBindingHolder {
        return onCreateViewBindingHolder(parent, viewType)
    }

    abstract fun onCreateViewBindingHolder(parent: ViewGroup, viewType: Int): ViewBindingHolder

    override fun getItemCount(): Int {
        if (this.data.isEmpty()) {
            onEmptyListener?.onEmpty()
            return 1
        }
        onEmptyListener?.onNotEmpty()
        return this.data.size
    }

    override fun onBindViewHolder(
        holder: ViewBindingHolder,
        @SuppressLint("RecyclerView") position: Int
    ) {
        if (data.isEmpty()){
            return
        }
        onItemListener?.apply {
            holder.binding.root.setOnClickListener {
                onItemClick(holder, getItem(position), position)
            }
        }
        onItemListener?.apply {
            holder.binding.root.setOnLongClickListener {
                onItemLongClick(holder, getItem(position), position)
                true
            }
        }
        onBindViewBindingHolder(holder, position)
    }

    abstract fun onBindViewBindingHolder(holder: ViewBindingHolder, position: Int)


    interface OnItemListener<V> {

        fun onItemClick(holder: ViewBindingHolder, v: V?, position: Int)

        fun onItemLongClick(holder: ViewBindingHolder, v: V?, position: Int): Boolean
    }

    interface OnEmptyListener {
        fun onEmpty()

        fun onNotEmpty()
    }
}